package hu.uszeged.inf.stud.prf.todo.entity.user;

import hu.uszeged.inf.stud.prf.todo.entity.resource.Resource;
import hu.uszeged.inf.stud.prf.todo.entity.task.TaskType;
import hu.uszeged.inf.stud.prf.todo.entity.usertask.UserTask;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class User implements Serializable {
	
	private static final long serialVersionUID = -5635645173372982375L;

	@Id
	@SequenceGenerator(name = "idGenSeqUser", initialValue=5)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenSeqUser")
	private Long id;
	
	private String name;
	
	private String password;
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
	private List<UserTask> usertasks;

	@OneToMany(mappedBy = "user")
	private List<Resource> resources;

	@OneToMany(mappedBy = "user")
	private List<TaskType> taskTypes;
	
	public User() {
		super();
	}

	public User(Long id, String name, String password, List<UserTask> tasks,
			List<Resource> resources) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.usertasks = tasks;
		this.resources = resources;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<UserTask> getUserTasks() {
		return usertasks;
	}

	public void setUserTasks(List<UserTask> tasks) {
		this.usertasks = tasks;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((resources == null) ? 0 : resources.hashCode());
		result = prime * result
				+ ((taskTypes == null) ? 0 : taskTypes.hashCode());
		result = prime * result
				+ ((usertasks == null) ? 0 : usertasks.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		
		return true;
	}

	@Override
	public String toString() {
		return name;
	}
	
	
}
